package com.cqust.controller;


import com.cqust.pojo.Employee;
import com.cqust.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


//    个人信息
    @RequestMapping("/myInfo/{eid}")
    public String myInfo(@PathVariable("eid") String eid,Model model){
        Employee employee = employeeService.getEmployee(eid);
        model.addAttribute("employee",employee);
        return "myInfo";
    }
//    全部员工信息
    @RequestMapping("/toMain")
    public String toMain(Model model){
        List<Employee> employees = employeeService.getEmployees();
        model.addAttribute("employees",employees);
        return "main";
    }
//    添加员工
    @PostMapping("/addEmployee")
    public String addEmployee(Employee employee, HttpSession session){
        employeeService.addEmployee(employee,session);
        return "redirect:/toMain";
    }
//    修改员工
    @PostMapping("/updateEmployee")
    public String updateEmployee(Employee employee,HttpSession session){
        employeeService.updateEmployee(employee,session);
        return "redirect:/toMain";
    }
    @PostMapping("/updateEmployee2")
    public String updateEmployee2(Employee employee,HttpSession session){
        employeeService.updateEmployee(employee,session);
        return "myInfo";
    }
    //    删除员工
    @RequestMapping("/deleteEmployee/{id}")
    public String deleteEmployee(@PathVariable("id") String id){
        employeeService.deleteEmployee(id);
        return "redirect:/toMain";
    }
//    模糊查询
    @PostMapping("/EmployeeLike")
    public String EmployeeLike(String name,String department,Model model){
        List<Employee> employees = employeeService.getEmployeeLike(name, department);
        if(employees == null) return "redirect:/toMain";
        model.addAttribute("employees",employees);
        return "main";
    }
}
